package edu.jmu.action.message;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import edu.jmu.action.BaseAction;
import edu.jmu.action.privilege.Permission;
import edu.jmu.bean.PageView;
import edu.jmu.bean.message.Message;
import edu.jmu.bean.message.MessageToUser;
import edu.jmu.bean.message.Message_Type;
import edu.jmu.bean.privilege.Privilege;
import edu.jmu.bean.privilege.Role;
import edu.jmu.bean.user.User;
import edu.jmu.service.message.MessageService;
import edu.jmu.service.message.MessageToUserService;
import edu.jmu.utils.WebUtil;

/***
 * 
 * @author Geven7
 *
 */
@Controller 
@Scope(value="prototype")
public class OperateMessageToUserAction extends BaseAction<MessageToUser> {
	private static final long serialVersionUID = -4976858584021126114L;
	@Resource
	private MessageToUserService messageToUserService;
	@Resource
	private MessageService messageService;
	private MessageToUser messageToUserBean;
	private Message messageBean;
	private int messageCount;
	private int sendmessageCount;
	private int messageUnreadCount;
	private Integer[] delIds;
	private int deleteId;
	List<MessageToUser> messageToUsers;
	private int showId;
	private int publishId;
	private String startDate;
	private String endDate;
	private String messageType;

	//获取全部消息
	public String messageManage(){
		this.pageView = new PageView<MessageToUser>(maxresult, currentpage);
		boolean noPass = false;
		
		for(Role role : WebUtil.getUser().getRoles()){
			for(Privilege myPrivilege : role.getPrivileges()){
				if(myPrivilege.getModule().equals("sysmanage") && myPrivilege.getPrivilege().equals("allMessage")){				
					noPass = true;
					break;
				}
			}
			if(!noPass)	break;
		}		
		if(noPass){
			messageManage2();
		}else{
			pageView.setQueryResult(messageToUserService.GetMessageToUserByFromUser(WebUtil.getUser()));			
		}
		return SUCCESS;
	}
	
	//如果是管理员权限获取全部发送和未发送消息
	@Permission(module="sysmanage",privilege="allMessage",desc="获取全部消息列表")
	private void messageManage2(){
		this.pageView = new PageView<MessageToUser>(maxresult, currentpage);
		pageView.setQueryResult(messageToUserService.getScrollData(pageView.getFirstResult(),pageView.getMaxresult()));		
	}
	
	//获取发件箱
	public String outbox(){
		String wherejpql = " o.fromUser = ?1  and o.message.ifPublish = 1";		
		this.pageView = new PageView<MessageToUser>(maxresult, currentpage);
		pageView.setQueryResult(messageToUserService.getScrollData(pageView.getFirstResult(),pageView.getMaxresult(),wherejpql,new User[]{WebUtil.getUser()}));
		return SUCCESS;
	}
	
	//获取收件箱
	public String inbox(){
		String wherejpql = " (o.toUser = ?1 or o.toUser is null) and (o.fromUser != ?2) and (o.message.ifPublish = 1)";		
		this.pageView = new PageView<MessageToUser>(maxresult, currentpage);
		pageView.setQueryResult(messageToUserService.getScrollData(pageView.getFirstResult(),pageView.getMaxresult(),wherejpql,new User[]{WebUtil.getUser(),WebUtil.getUser()}));
		return SUCCESS;
	}
	
	//根据时间查询发送消息
	//@Permission(module="message",privilege="messageListByTime",desc="根据时间查询发送消息")
	public String messageListByTime(){
		pageView = new PageView<MessageToUser>(maxresult,currentpage);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		if(startDate !=null || endDate != null || "".equals(startDate) || "".equals(endDate)){
			String wherejpql = "o.message.recordTime >=?1 and o.message.recordTime <=?2";
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			try {
				pageView.setQueryResult(messageToUserService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(),wherejpql,new Date[]{dateFormat.parse(startDate),dateFormat.parse(endDate)},orderby));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else{
			pageView.setQueryResult(messageToUserService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(), orderby));
		}
		return SUCCESS;
	}

	//展示要修改的messageToUser
	public String showUpdateMessageToUser(){
		//this.messageToUserBean = messageToUserService.find(messageToUserBean.getId());
		String wherejpql = "o.id = ?1";
		this.messageToUserBean = messageToUserService.getScrollData(pageView.getFirstResult(), pageView.getCurrentpage(), wherejpql, new MessageToUser[]{messageToUserBean}).getResultlist().get(0);
		return SUCCESS;
	}
	
	//获取数目
	public String messageBox(){
		messageCount = (int) messageToUserService.GetMessageToUserCountByToUserName(WebUtil.getUser());
		sendmessageCount = (int)messageToUserService.GetMessageToUserCountByFromUserName(WebUtil.getUser());
		messageUnreadCount = (int) messageToUserService.GetMessageToUserUnreadCountByToUserName(WebUtil.getUser());
		return SUCCESS;
	}
	
	//CUD
	//根据id批量删除messageToUser
	public String deleteMessageToUserByIds(){
		messageToUserService.delete((Serializable[])delIds);
		return SUCCESS;
	}
	
	//增加消息并创建多条messageToUser
	public String addMessageToUser(){
		
		MessageToUser messageToUser2 = new MessageToUser();
		messageToUser2.setFromUser(WebUtil.getUser());
		messageToUser2.setMessage(messageBean);
		if("General".equals(messageType)	){
			messageBean.setMessageType(Message_Type.General);
		}else{
			messageBean.setMessageType(Message_Type.Urgent);
		}
		messageBean.addMessageToUser(messageToUser2);
		messageBean.setIfPublish(0);
		
		// save message and cascade
		messageService.save(messageBean);
		return SUCCESS;
	}
	
	//更新messageToUser
	public String updateMessageToUser(){
		Message message = messageService.find(messageToUserBean.getMessage().getId());
		messageToUserBean.getMessage().setIfPublish(message.getIfPublish());
		messageService.update(messageToUserBean.getMessage());
		return SUCCESS;
	}
	
	//删除一条messageToUser
	public String deleteMessageToUser(){
		messageToUserService.delete(deleteId);
		return SUCCESS;
	}
	
	//展示一条messageToUser
	public String showMessageToUser(){
		this.messageToUserBean = messageToUserService.find(showId);
		return SUCCESS;
	}
	
	//展示一条messageToUser
		public String updateIsRead(){
			this.messageToUserBean = messageToUserService.find(showId);
			this.messageToUserBean.setIsRead(1);
			messageToUserService.update(this.messageToUserBean);
			return SUCCESS;
		}
	
	//发布消息
	public String publishMessage(){
		this.messageToUserBean = messageToUserService.find(publishId);
		Message message = messageService.find(messageToUserBean.getMessage().getId());
		messageToUserService.publishMessage(message);
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//Getter && Setter
	public int getMessageUnreadCount() {
		return messageUnreadCount;
	}
	
	public int getMessageCount() {
		return messageCount;
	}

	public MessageToUser getMessageToUserBean() {
		return messageToUserBean;
	}

	public void setMessageToUserBean(MessageToUser messageToUserBean) {
		this.messageToUserBean = messageToUserBean;
	}

	public Integer[] getDelIds() {
		return delIds;
	}

	public void setDelIds(Integer[] delIds) {
		this.delIds = delIds;
	}

	public int getSendmessageCount() {
		return sendmessageCount;
	}

	public Message getMessageBean() {
		return messageBean;
	}

	public void setMessageBean(Message messageBean) {
		this.messageBean = messageBean;
	}

	public List<MessageToUser> getMessageToUsers() {
		return messageToUsers;
	}

	public void setMessageToUsers(List<MessageToUser> messageToUsers) {
		this.messageToUsers = messageToUsers;
	}

	public String getStartDate() {
		return startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public int getDeleteId() {
		return deleteId;
	}

	public void setDeleteId(int deleteId) {
		this.deleteId = deleteId;
	}

	public void setShowId(int showId) {
		this.showId = showId;
	}

	public void setPublishId(int publishId) {
		this.publishId = publishId;
	}

	public void setMessageType(String messageType) {
		this.messageType = messageType;
	}

}